﻿#region Imports
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.Linq;
using Jounce.Core.Command;
using Jounce.Core.View;
using Jounce.Core.ViewModel;
using Jounce.Framework;
using Jounce.Framework.Command;
using JounceSilverlightDemo.Models.Entity;
#endregion

namespace JounceSilverlightDemo.ViewModels
{
    [ExportAsViewModel("LoginVM")]
    public class LoginViewModel : BaseViewModel, IPartImportsSatisfiedNotification
    {
        #region DummyData
        ObservableCollection<User> UserInfo = null;
        private void PopulateDummyData()
        {
            UserInfo = new ObservableCollection<User>();

            UserInfo.Add(new User { UserName = "Rupak", UserPassword = "123" });
            UserInfo.Add(new User { UserName = "Test_user", UserPassword = "123" });
        }
        #endregion

        #region Command declarations
        public IActionCommand<object> LoginCommand { get; private set; }
        #endregion

        #region Properties
        private string userName;
        public string UserName
        {
            get { return userName; }
            set { userName = value; RaisePropertyChanged("UserName"); ValidateCreds(); }
        }

        private string userPassword;
        public string UserPassword
        {
            get { return userPassword; }
            set { userPassword = value; RaisePropertyChanged("UserPassword"); ValidateCreds(); }
        }

        private bool isLoginAllowed = false;
        public bool IsLoginAllowed
        {
            get { return isLoginAllowed; }
            set { isLoginAllowed = value; RaisePropertyChanged("IsLoginAllowed"); }
        }

        private bool hasError;
        public bool HasError
        {
            get { return hasError; }
            set { hasError = value; RaisePropertyChanged("HasError"); }
        }
        #endregion

        #region Constructors
        public LoginViewModel()
        {
            PopulateDummyData();
        }
        #endregion

        #region Core methods
        public void OnImportsSatisfied()
        {
            LoginCommand = new ActionCommand<object>(UserLogin);
        }

        protected override void InitializeVm()
        {
            base.InitializeVm();
        }

        protected override void ActivateView(string viewName, IDictionary<string, object> viewParameters)
        {
            UserName = UserPassword = "";
        }
        #endregion

        #region Methods
        private void UserLogin(object parameter)
        {
            if (UserInfo.Where(usr => usr.UserName.ToLower() == UserName.ToLower() && usr.UserPassword == UserPassword).Any())
            {
                Dictionary<string, object> paramdict = new Dictionary<string, object>();
                EventAggregator.Publish(new ViewNavigationArgs("HeaderPage", paramdict)
                   .AddNamedParameter("UserName", UserName));

                Go_to_new_page;
            }
            else
                HasError = true;
        }

        private void ValidateCreds()
        {
            IsLoginAllowed = !String.IsNullOrEmpty(UserName) && !String.IsNullOrEmpty(UserPassword);
        }
        #endregion
    }
}
